package com.souku.finance.application.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.bobomee.android.scrollloopviewpager.autoscrollviewpager.AutoScrollViewPager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.souku.finance.application.R;
import com.souku.finance.application.adapter.MyImageAdapter;
import com.souku.finance.application.base.ActivityManage;
import com.souku.finance.application.base.BaseFragment;
import com.souku.finance.application.bean.IndexBean;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.ui.invite.TenderDetailsActivity;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.CircleViewHome;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.SuccessError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import me.crosswall.lib.coverflow.CoverFlow;
import me.crosswall.lib.coverflow.core.PagerContainer;

/**
 * Created by Android on 2016/12/5.
 */

public class HomeFragment extends BaseFragment implements View.OnClickListener {

    protected View rootView;
    protected CircleViewHome circle;
    protected TextView circleTv;
    protected TextView tvTitle;
    protected PullToRefreshScrollView pullView;
    protected TextView tvDealName;
    protected RelativeLayout rlIsnew;
    protected TextView tvDealDate;
    protected TextView tvLimit;
    protected TextView tvMin;
    protected RelativeLayout rlDeal;
    protected TextView tvNo;
    private PagerContainer banner;
    private AutoScrollViewPager pager;
    private IndexBean bean;
    private List<IndexBean.BannersBean> list = new ArrayList<>();
    private MyImageAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fra_home, container, false);
        setContent(rootView);
        initView(rootView);
        initViewPage();
        pullView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                getIndex();
            }
        });
        return rootView;
    }

    private void initView(View rootView) {
        banner = (PagerContainer) rootView.findViewById(R.id.banner);
        pager = banner.getViewPager();
        circle = (CircleViewHome) rootView.findViewById(R.id.circle);
        circleTv = (TextView) rootView.findViewById(R.id.circle_tv);
        circle.setStoke(getActivity());
        tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
        pullView = (PullToRefreshScrollView) rootView.findViewById(R.id.pullView);
        tvDealName = (TextView) rootView.findViewById(R.id.tv_dealName);
        rlIsnew = (RelativeLayout) rootView.findViewById(R.id.rl_isnew);
        tvDealDate = (TextView) rootView.findViewById(R.id.tv_dealDate);
        tvLimit = (TextView) rootView.findViewById(R.id.tv_limit);
        tvMin = (TextView) rootView.findViewById(R.id.tv_min);
        rlDeal = (RelativeLayout) rootView.findViewById(R.id.rl_deal);
        tvNo = (TextView) rootView.findViewById(R.id.tv_no);
    }

    private void initViewPage() {
        if (list == null) {
            return;
        }
        adapter = new MyImageAdapter(getActivity(), list, new MyImageAdapter.OnImageViewClick() {
            @Override
            public void click(String url) {
                if (url != null) {
                    ActivityManage.startBaseWebActivity(getActivity(), "", url);
                }
            }
        });
        pager.setAdapter(adapter);
        pager.setClipChildren(false);
        pager.setOffscreenPageLimit(15);
        pager.startAutoScroll(3000);
        pager.setInterval(3000);
        new CoverFlow.Builder()
                .with(pager)
                .scale(0.3f)
                .pagerMargin(getResources().getDimensionPixelSize(R.dimen.pager_margin))
                .spaceSize(0f)
                .build();
    }

    @Override
    public void internet() {
        pullView.setRefreshing(true);
    }

    @Override
    public void showFragment() {
        if (bean == null) {
            pullView.setRefreshing(true);
        }
    }

    private void getIndex() {
        HashMap<String, String> map = ParamUtil.getParams();
        RequestManager.getInstance().post(UrlConstact.INDEX, ParamUtil.getSing(map), new ReqListener(getActivity(), new SuccessError() {
            @Override
            public void onSuccess(String response) {
                pullView.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), IndexBean.class);
                    setData();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                pullView.onRefreshComplete();
                ToastUtil.show("网络错误");
            }
        }), ParamUtil.getHeaders());
    }

    private void setData() {
        list = bean.getBanners();
        initViewPage();
        circle.setPrograss(Integer.parseInt(bean.getInvestmentAmount()), Integer.parseInt(bean.getBorrowAmount()), tvNo);
        MyTextUtil.setType(getContext(), circleTv);
        circleTv.setText(bean.getRate() + "%");
        tvDealName.setText(bean.getDealName());
        tvLimit.setText(MyTextUtil.setMoneyToElement(Integer.parseInt(bean.getBorrowAmount()) - Integer.parseInt(bean.getInvestmentAmount()) + ""));
        tvMin.setText(MyTextUtil.setMoneyToElement(bean.getMinInvestment()));
        if (bean.getIsNew().equals("1")) {
            rlIsnew.setVisibility(View.VISIBLE);
        } else {
            rlIsnew.setVisibility(View.GONE);
        }
        if (bean.getDealDateType().equals("1")) {
            tvDealDate.setText(Integer.parseInt(bean.getDealDate()) * 30 + "天");
        } else {
            tvDealDate.setText(Integer.parseInt(bean.getDealDate()) + "天");
        }
        rlDeal.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.rl_deal) {
            Intent intent = new Intent(getActivity(), TenderDetailsActivity.class);
            intent.putExtra(Constant.Default, bean.getId());
            startActivity(intent);
        }
    }
}
